/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MayTinh.java
 *
 */
package cacula;

/**
 *
 * @author quanghuy
 */
import java.awt.event.*;
public class MayTinh extends javax.swing.JFrame implements ActionListener {
   static String bieuThuc="";
   static String hauTo="";
   static Stack stack = new Stack();
   static boolean bang = false;
   static boolean error = false;
   static float dapAn;
    /** Creates new form MayTinh */
    public MayTinh() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tf = new javax.swing.JTextField();
        b1 = new javax.swing.JButton();
        b2 = new javax.swing.JButton();
        b3 = new javax.swing.JButton();
        b4 = new javax.swing.JButton();
        b5 = new javax.swing.JButton();
        b6 = new javax.swing.JButton();
        b7 = new javax.swing.JButton();
        b8 = new javax.swing.JButton();
        b9 = new javax.swing.JButton();
        del = new javax.swing.JButton();
        mul = new javax.swing.JButton();
        div = new javax.swing.JButton();
        sub = new javax.swing.JButton();
        b0 = new javax.swing.JButton();
        ac = new javax.swing.JButton();
        add = new javax.swing.JButton();
        point = new javax.swing.JButton();
        can = new javax.swing.JButton();
        pow = new javax.swing.JButton();
        equals = new javax.swing.JButton();
        kq = new javax.swing.JTextField();
        sin = new javax.swing.JButton();
        cos = new javax.swing.JButton();
        tan = new javax.swing.JButton();
        mNg = new javax.swing.JButton();
        dNg = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tf.setFont(new java.awt.Font("Tahoma", 1, 14));
        tf.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tf.setEnabled(false);

        b1.setFont(new java.awt.Font("Tahoma", 1, 14));
        b1.setText("1");

        b2.setFont(new java.awt.Font("Tahoma", 1, 14));
        b2.setText("2");

        b3.setFont(new java.awt.Font("Tahoma", 1, 14));
        b3.setText("3");

        b4.setFont(new java.awt.Font("Tahoma", 1, 14));
        b4.setText("4");

        b5.setFont(new java.awt.Font("Tahoma", 1, 14));
        b5.setText("5");

        b6.setFont(new java.awt.Font("Tahoma", 1, 14));
        b6.setText("6");

        b7.setFont(new java.awt.Font("Tahoma", 1, 14));
        b7.setText("7");

        b8.setFont(new java.awt.Font("Tahoma", 1, 14));
        b8.setText("8");

        b9.setFont(new java.awt.Font("Tahoma", 1, 14));
        b9.setText("9");

        del.setFont(new java.awt.Font("Tahoma", 1, 14));
        del.setText("DEL");

        mul.setFont(new java.awt.Font("Tahoma", 1, 14));
        mul.setText("*");

        div.setFont(new java.awt.Font("Tahoma", 1, 14));
        div.setText("/");

        sub.setFont(new java.awt.Font("Tahoma", 1, 14));
        sub.setText("-");

        b0.setFont(new java.awt.Font("Tahoma", 1, 14));
        b0.setText("0");

        ac.setFont(new java.awt.Font("Tahoma", 1, 14));
        ac.setText("AC");

        add.setFont(new java.awt.Font("Tahoma", 1, 14));
        add.setText("+");

        point.setFont(new java.awt.Font("Tahoma", 1, 14));
        point.setText(".");

        can.setFont(new java.awt.Font("Tahoma", 1, 14));
        can.setText("\\/'''");

        pow.setFont(new java.awt.Font("Tahoma", 1, 14));
        pow.setText("^");

        equals.setFont(new java.awt.Font("Tahoma", 1, 14));
        equals.setText("=");

        kq.setEditable(false);
        kq.setFont(new java.awt.Font("Tahoma", 1, 14));
        kq.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        kq.setText("0");
        kq.setEnabled(false);

        sin.setFont(new java.awt.Font("Tahoma", 1, 14));
        sin.setText("sin");

        cos.setFont(new java.awt.Font("Tahoma", 1, 14));
        cos.setText("cos");

        tan.setFont(new java.awt.Font("Tahoma", 1, 14));
        tan.setText("tan");

        mNg.setFont(new java.awt.Font("Tahoma", 1, 14));
        mNg.setText("(");

        dNg.setFont(new java.awt.Font("Tahoma", 1, 14));
        dNg.setText(")");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(b0, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(point, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(b4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(del)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(ac))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(add, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(mul, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(div, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                                            .addComponent(sub, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(pow, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(can, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)))
                        .addGap(40, 40, 40))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(sin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cos, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kq, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(tan)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(mNg)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dNg)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(tf, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(kq, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dNg, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mNg, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tan, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cos, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sin, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(del, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ac, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mul, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(div, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sub, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b0, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(point, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pow, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(can, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(equals, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        del.addActionListener(this);
        mul.addActionListener(this);
        div.addActionListener(this);
        sub.addActionListener(this);
        b0.addActionListener(this);
        ac.addActionListener(this);
        add.addActionListener(this);
        point.addActionListener(this);
        can.addActionListener(this);
        pow.addActionListener(this);
        equals.addActionListener(this);
        sin.addActionListener(this);
        cos.addActionListener(this);
        tan.addActionListener(this);
        mNg.addActionListener(this);
        dNg.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setTitle("Calculator by HoangTuTimBang");
        setLocationRelativeTo(null);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    @Override
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b0 || ae.getSource()==b1 || ae.getSource()==b2 || ae.getSource()==b3 ||
                ae.getSource()==b4 || ae.getSource()==b5 || ae.getSource()==b6 || ae.getSource()==b7 ||
                ae.getSource()==b8 || ae.getSource()==b9 || ae.getSource()==mul || ae.getSource()==div ||
                ae.getSource()==add || ae.getSource()==sub || ae.getSource()==point || ae.getSource()==pow
                || ae.getSource()==mNg || ae.getSource()==dNg || ae.getSource()==sin || ae.getSource()==cos || ae.getSource()==tan)
        {
            if(!bang) bieuThuc = tf.getText();
            else if((ae.getSource()==mul || ae.getSource()==div ||
                ae.getSource()==add || ae.getSource()==sub || ae.getSource()==pow) && !error) bieuThuc= kq.getText(); else bieuThuc="";
            bieuThuc+= ae.getActionCommand();
            tf.setText(bieuThuc);
            bang= false;
        }
        if(ae.getSource()==equals)
        {
            bang=true;
            inRa();
            
        }
        if(ae.getSource()==del && !bang)
        {
            if(bieuThuc.length()>0)
            {
                bieuThuc= bieuThuc.substring(0,bieuThuc.length()-1);
                tf.setText(bieuThuc);
            }
            
        }
        if(ae.getSource() == ac) 
        {
            bieuThuc = ""; 
            tf.setText(""); kq.setText("0");
            bang = false;
        }
        if(ae.getSource()==can) 
        {
            bieuThuc+= "\\";
            tf.setText(bieuThuc);
        }
    }
    /**
     * @param args the command line arguments
     */
    public boolean checkDau(char x)
    {
        if(x=='+' || x=='-' || x=='*' || x=='/' || x=='(' || x=='^') return true;
        return false;
    }
    public String layDau(String str, int i)
    {
        String dau="";
     try{
          while((str.charAt(i)<'0' || str.charAt(i)>'9') && i<str.length())
          {
            dau+=String.valueOf(str.charAt(i));
            i++;
          } 
      }catch(Exception e){dau="/";}
        return dau;
    }
    public void chuyenHauTo(String bt1)
    {
        int i=0;
        hauTo="";
        stack.top=0;
        while(i<bt1.length())
        {
            if(bt1.charAt(i)=='(') stack.push(String.valueOf(bt1.charAt(i)));
            else if(bt1.charAt(i)==')')
            {
                String tam;
                try{ 
                     do{
                    tam = stack.pop();
                    if(tam.compareTo("(")!=0) hauTo+=" "+tam;
                }while(tam.compareTo("(")!=0);
                }catch(Exception e)
                {
                    hauTo=""; return;
                }
            }
            else if(checkDau(bt1.charAt(i)) || bt1.charAt(i)=='\\')
            {  
                if((i>0 && checkDau(bt1.charAt(i-1)) || i==0 || bt1.charAt(i-1)=='s' || bt1.charAt(i-1)=='n') && bt1.charAt(i)!='\\' )
                {
                    String dau = "";
                    dau = layDau(bt1,i);
                    int dem=0;
                    for(int j=0;j<dau.length();j++)
                        if(dau.charAt(j)!='+' && dau.charAt(j)!='-')
                            {
                             hauTo="";
                             return;
                            } else if(dau.charAt(j)=='-') dem++;
                    if(dem%2!=0) hauTo+= " -"; else hauTo+= " ";
                    i+= dau.length();
                    try{
                        while(bt1.charAt(i)>='0' && bt1.charAt(i)<='9' || bt1.charAt(i)=='.' )
                        {
                           hauTo+=String.valueOf(bt1.charAt(i));
                           i++;
                        } 
                      }catch(Exception e){ }
                    i--;
                }
                else{
                    while(!stack.isEmpty() && stack.Prority(String.valueOf(bt1.charAt(i)))<= stack.Prority(stack.get()))
                    hauTo+=" "+stack.pop();
                   stack.push(String.valueOf(bt1.charAt(i)));
                }
                
            } else
            if(bt1.charAt(i)=='s' || bt1.charAt(i)=='c' || bt1.charAt(i)=='t') 
            {
                stack.push(String.valueOf(bt1.charAt(i)));
                i=i+2;
            } else 
            if((bt1.charAt(i)>='0' && bt1.charAt(i)<='9') || bt1.charAt(i)=='.')
            {
                hauTo+=" ";
                try{
                    while((bt1.charAt(i)>='0' && bt1.charAt(i)<='9') || bt1.charAt(i)=='.' && i<bt1.length())
                    {
                        hauTo+= String.valueOf(bt1.charAt(i));
                        i++;
                    }
                    i--;
                }catch(Exception e){}
            }
            i++;
        }
        while(!stack.isEmpty()) hauTo+= " "+stack.pop();
    }
    public void TinhBT()
    {
        float x=0.0f,y;
        int z;
        z = bieuThuc.indexOf("()");
        error = false;
        if(z>=0)
        {
            kq.setText("Error");
            error = true;
            return;
        }    
        chuyenHauTo(bieuThuc);
        hauTo = hauTo.trim();
        System.out.println(hauTo);
        if(hauTo.length()==0 || bieuThuc.endsWith("+") || bieuThuc.endsWith("-") || bieuThuc.endsWith("*")
                || bieuThuc.endsWith("/") || bieuThuc.endsWith("^") || bieuThuc.endsWith("n") 
                || bieuThuc.endsWith("s") || bieuThuc.endsWith("\\"))
        {
            kq.setText("Error");
            error = true;
            return;
        }
        int i=0;
        String[] dayso = hauTo.split(" ");
        stack.top=0;
        while(i < dayso.length)
        {
            if(dayso[i].compareTo("+")==0)
            {
                    try{
                        x = Float.parseFloat(stack.pop());
                        y = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                    x = x+y;
                    stack.push(String.valueOf(x));
            }
            else if(dayso[i].compareTo("-")==0)
            {
                    try{
                        x = Float.parseFloat(stack.pop());
                        y = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                    x = y-x;
                    stack.push(String.valueOf(x));
            }
            else if(dayso[i].compareTo("*")==0)
            {
                    try{
                        x = Float.parseFloat(stack.pop());
                        y = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                    x = y*x;
                    stack.push(String.valueOf(x));
            }
            else if(dayso[i].compareTo("/")==0)
            {
                    try{
                        x = Float.parseFloat(stack.pop());
                        y = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                    x = y/x;
                    stack.push(String.valueOf(x));
            } 
            else if(dayso[i].compareTo("^")==0)
            {
                    try{
                        x = Float.parseFloat(stack.pop());
                        y = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                    x = (float)Math.pow((double)y, (double)x);
                    stack.push(String.valueOf(x));
            } 
            else if(dayso[i].compareTo("s")==0)
            {
                try{
                        x = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                x = (float)Math.sin(Math.PI*(x)/180);
                stack.push(String.valueOf(x));      
            } else if(dayso[i].compareTo("c")==0)
            {
                try{
                        x = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                x = (float)Math.cos(Math.PI*x/180);
                stack.push(String.valueOf(x));      
            } else if(dayso[i].compareTo("t")==0)
            {
                try{
                        x = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                x = (float)Math.tan(Math.PI*x/180);
                stack.push(String.valueOf(x));      
            }else if(dayso[i].compareTo("\\")==0)
            {
                try{
                        x = Float.parseFloat(stack.pop());
                    }catch(Exception e){
                        error = true;
                        kq.setText("Error");
                        return;
                    }
                x = (float)Math.sqrt((double)x);
                stack.push(String.valueOf(x));      
            }
            else stack.push(dayso[i]);
            i++;
        }
        if(stack.top==1)
        {
            try{
                x = Float.parseFloat(stack.pop());
            }catch(Exception e)
            {
                error = true;
                kq.setText("Error");
                return;
            }
        } else 
        {
            error = true;
            kq.setText("Error");
            return;
        }
        dapAn = x;
    }
    public void inRa()
    {
        TinhBT();
        int z;
        if(!error)
        {
            z = (int)dapAn;
            if(dapAn!=z) kq.setText(String.valueOf(dapAn));
             else kq.setText(String.valueOf(z));
        } 
    }
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MayTinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MayTinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MayTinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MayTinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MayTinh().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ac;
    private javax.swing.JButton add;
    private javax.swing.JButton b0;
    private javax.swing.JButton b1;
    private javax.swing.JButton b2;
    private javax.swing.JButton b3;
    private javax.swing.JButton b4;
    private javax.swing.JButton b5;
    private javax.swing.JButton b6;
    private javax.swing.JButton b7;
    private javax.swing.JButton b8;
    private javax.swing.JButton b9;
    private javax.swing.JButton can;
    private javax.swing.JButton cos;
    private javax.swing.JButton dNg;
    private javax.swing.JButton del;
    private javax.swing.JButton div;
    private javax.swing.JButton equals;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField kq;
    private javax.swing.JButton mNg;
    private javax.swing.JButton mul;
    private javax.swing.JButton point;
    private javax.swing.JButton pow;
    private javax.swing.JButton sin;
    private javax.swing.JButton sub;
    private javax.swing.JButton tan;
    private javax.swing.JTextField tf;
    // End of variables declaration//GEN-END:variables
}
class Stack
{
    String[] s = new String[100];
    int top=0;
    void push(String x)
    {
        s[++top]=x;
    }
    String pop()
    {
        return s[top--];
    }
    String get()
    {
        return s[top];
    }
    boolean isEmpty()
    {
        if(top==0) return true;
        return false;
    }
    int Prority(String x)
    {
        if(x.compareTo("(")==0 || x.compareTo(")")==0) return 0;
        if(x.compareTo("+")==0 || x.compareTo("-")==0) return 1;
        if(x.compareTo("*")==0 || x.compareTo("/")==0) return 2;
        if(x.compareTo("^")==0) return 3;
        if(x.compareTo("s")==0 || x.compareTo("c")==0 || x.compareTo("t")==0 ) return 4;
        if(x.compareTo("\\")==0) return 5;
        return -1;
    }
}